<?php
// http://www.12301.cc/module/zj_terminal/terminalCheck.php?ac=123757&pw=828074ac9f9d648a2eb2992fc33aecb8&sid=616007&code=287205
// http://www.12301.cc/module/zj_terminal/terminalCheck.php?ac=123757&pw=828074ac9f9d648a2eb2992fc33aecb8&sid=617604&code=724465
// http://www.12301.cc/a/a.php?ac=200566&pw=828074ac9f9d648a2eb2992fc33aecb8&sid=602288&code=165993

include_once '/var/www/html/new/com.inc.php';

if($_REQUEST['ac']=='')   exit('{"code":"1011","msg":"请输入授权账号"}');
if($_REQUEST['pw']=='')   exit('{"code":"1011","msg":"请输入授权密码"}');
if($_REQUEST['sid']=='')  exit('{"code":"1011","msg":"请输入景区ID"}');
if($_REQUEST['code']=='') exit('{"code":"1011","msg":"请输入凭证码"}');


$start_time = microtime(true);

// 权限判断
$auth_arr = include '/var/www/html/new/d/module/zj_terminal/zj_permissions.php';
$recv_ac = $_REQUEST['ac'];
$recv_pw = $_REQUEST['pw'];
if(!in_array($recv_ac, $auth_arr)) exit('{"code":"1011","msg":"无授权，拒绝连接"}');


// 根据账号生成接口编码
$sql="select dname,id from pft_member where account='$recv_ac' limit 1";
$GLOBALS['le']->query($sql);
if(!$GLOBALS['le']->fetch_assoc()) exit('{"code":"1011","msg":"账号不存在"}');
$dname     = $GLOBALS['le']->f('dname');
$memberSID = $GLOBALS['le']->f('id');



// 实例化内部接口对象
$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$chpw  = $s->Create_Conn_REC($recv_ac, $dname);
if($recv_pw!=$chpw) exit('{"code":"1011","msg":"无授权，拒绝连接"}');



// 订单归属判断
$salerid = $_REQUEST['sid'];
$sel = "select apply_did,terminal,terminal_type from uu_land where salerid='$salerid' limit 1";
$GLOBALS['le']->query($sel);
if(!$GLOBALS['le']->fetch_assoc()) exit('{"code":"1011","msg":"景区不存在"}');
if($memberSID!=$GLOBALS['le']->f('apply_did')) exit('{"code":"4002","msg":"非自供订单，不能验证"}');

$terminal_inum = $GLOBALS['le']->f('terminal');
$terminal_type = $GLOBALS['le']->f('terminal_type');

if($terminal_type==0){// 旧方式验证
	$time = date("Y-m-d H:i:s");
	$result = $s->Terminal_Check_In($salerid, $_REQUEST['code'], null, '');
	if(is_numeric($result)){
		logfile($result.'----runtime---'.microtime(true) - $start_time);
		$repMsg = Err_Word($result);
		exit('{"code":"'.$result.'","msg":"'.$repMsg.'"}');
	}
	
	$simxml   = simplexml_load_string($result);
	$ordernum = (string)$simxml->Rec->UUordernum;
	logfile("[$ordernum]订单验证,执行耗时:".(microtime(true) - $start_time));
}else{// 新终端服务验证
	include_once('/var/www/html/new/d/class/Terminal_Check_Socket.class.php');
	$tc = Terminal_Check_Socket::connect();
	$chResult = $tc->Terminal_Check_In_Voucher($terminal_inum, $salerid, $_REQUEST['code'],array("vMode"=>5));
	$checkData = json_encode(array($terminal_inum, $salerid, $_REQUEST['code']));
	if($chResult['state']=='fail' || isset($chResult['multi'])){// 多单也返回错误
		$logs = "订单验证,数据[$checkData],结果[".json_encode($chResult)."],执行时间[".(microtime(true)-$start_time)."]";
		logfile($logs, '/var/www/html/new/d/module/zj_terminal/terminal_check_new_error.log');
		exit('{"code":"101","msg":"'.$chResult['msg'].'"}');
	}
	
	$ordernum = $chResult['ordernum'];// 获得订单号
	
	// 成功日志
	$logs = "[$ordernum]订单验证,数据[$checkData],结果[".json_encode($chResult)."],执行时间[".(microtime(true)-$start_time)."]";
	logfile($logs,'/var/www/html/new/d/module/zj_terminal/terminal_check_new.log');
}

// 二级分销商
$resxml = $s->Order_Globle_Search('', '', '', '', '', '', '', '', '', '', '', '', $ordernum, '', '', '', '', '', 1, 1, 0, 1);
$simxml = simplexml_load_string($resxml);
$buyid  = (string)$simxml->Rec->UUbuyid;
if((string)$simxml->Rec->UUaids!=0){
	$aids  = explode(',', (string)$simxml->Rec->UUaids);
	$buyid = $aids[1];
}
$sql="select dname from pft_member where id=$buyid limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc(); 
$fendname = $GLOBALS['le']->f('dname');
// echo '<pre/>';
$response = array();
$response['code']        = 100;
$response['msg']         = '验证成功';
$response['time']        = date('Y-m-d H:i:s');
$response['UUordernum']  = $ordernum;
$response['Distributor'] = $fendname;
$response['UUcode']      = (string)$simxml->Rec->UUcode;
// $response['UUtnum']      = (string)$simxml->Rec->UUtnum;
// $response['UUtprice']    = (string)$simxml->Rec->UUtprice;
// $response['UUttitle']    = (string)$simxml->Rec->UUttitle;
$response['UUordertel']  = (string)$simxml->Rec->UUordertel;
$response['UUordername'] = (string)$simxml->Rec->UUordername;
$response['UUpaystatus'] = (string)$simxml->Rec->UUpaystatus;
if($terminal_type==1) 
	$response['tickets'] = $chResult['tickets'];
else{
	$concat_id = (string)$simxml->Rec->UUconcat_id;
	if($concat_id!=0 && $concat_id==$ordernum){
		$resxml = $s->Order_Globle_Search("","","","","","","","","","","","",$ordernum,"","","","","",1,1,0,10,"","","","","","","","","",1,0);
		$simxml = simplexml_load_string($resxml);
		foreach($simxml->Rec as $row){
			$response['tickets'][] = array('ticket_name'=>(string)$row->UUttitle,'ticket_num'=>(int)$row->UUtnum);
		}
	}
}
// print_r($response);
exit(json_encode($response));



function Err_Word($r){
	$errCode = array(
		'105'=>'找不到订单',
		'121'=>'订单状态出错,订单已验证',
		'119'=>'订单状态出错,已取消或其他状态',
		'521'=>'验证间隔时间未到',
		'133'=>'游玩时间未到',
		'134'=>'游玩时间已过',
		'118'=>'终端服务器无响应',
		'1070'=>'订单未支付',
	);
	$eword=$errCode[$r];
	if (!$eword) $eword='其他错误';
	return $eword;
}

function logfile($txt, $file = '/var/www/html/new/d/module/zj_terminal/New_zd_log.txt'){
	$fp = fopen($file, "a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp); 
}